/**
 * 
 */
package vn.nnn.shogi.quanco;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JApplet;

import at.com.vn.chess.util.RectangleUtils;

import vn.nnn.shogi.board.Board;


/**
 * 
 */
public class QuanVua extends QuanCo {

	public QuanVua( Rectangle toaDo, int color, boolean normalDirection, JApplet mainApplet ) {
		super( toaDo, color, normalDirection, mainApplet );	
	}
	
	@Override
	public void setToaDoNhaTu() {
		toaDoNhaTu = new Point( 0, 0 );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canMove(java.awt.Rectangle, vn.nnn.shogi.quanco.QuanCo, java.util.ArrayList)
	 */
	@Override
	public boolean canMove( Board board, Rectangle destination,
			QuanCo foundedPiece, ArrayList<QuanCo> listQuanCo ) {
		boolean canmove = false;
		
		// Neu la hai hang tren va duoi		
		if( RectangleUtils.isNextRow( toaDo, destination )
				|| RectangleUtils.isNextRow( destination, toaDo ) ) {
			if( RectangleUtils.isInSameColumn( toaDo, destination )
					|| RectangleUtils.isColumnBeside( toaDo, destination ) ) {
				canmove = true;
			}			
		}
		
		// Neu la cung hang va la cot ben canh
		else if( RectangleUtils.isInSameRow( toaDo, destination ) 
					&& RectangleUtils.isColumnBeside( toaDo, destination ) ) {
			canmove = true;
		}
		
		// Dieu kien co ban khi di quan
		if ( canmove ) {
			canmove = isPassToBasicMoveCondition( board, destination, foundedPiece, listQuanCo );
		}
		
		return canmove;	
	}
	
	@Override
	public boolean canUpgrade() {
		return false;
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#upgrade(java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public void upgrade( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canRelease(vn.nnn.shogi.quanco.Board, java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public boolean canRelease( Board board, Rectangle destination,
			ArrayList<QuanCo> listQuanCo ) {		
		return false;
	}
	
	/**
	 * Neu cac quan co doi thu co the di chuyen duoc den vi tri cua vua nay
	 * thi co nghia la quan vua dang bi chieu
	 * @param board
	 * @param listQuanCo
	 * @return
	 */
	public boolean isDangBiChieu( Board board, ArrayList<QuanCo> listQuanCo ) {
		for( QuanCo quanCo : listQuanCo ) {
			if( quanCo.color != color && quanCo.canMove( board, toaDo, null, listQuanCo ) ) {
				return true;
			}
		}
		return false;
	}
	
	public QuanCo findQuanDangChieu( Board board, ArrayList<QuanCo> listQuanCo ) {

		for( QuanCo quanCo : listQuanCo ) {
			if( quanCo.color == getEnemyColor() && quanCo.canMove( board, toaDo, this, listQuanCo ) ) {
				return quanCo;
			}
		}
		
		return null;
	}
}
